#include <iostream>
#include "opencv2/opencv.hpp"
#include "face.h"
#include <time.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <string.h>



using namespace std;
using namespace cv;
using namespace aip;





int main()
{

    std::string app_id = "41970265";
    std::string api_key = "X12HM2boqf8uqZffVu07Ed6b";
    std::string secret_key = "fzgXN8daLjKlv5lQRfi1IkwzhM1gB0Xn";

    aip::Face client(app_id,api_key,secret_key);

    //1.打开摄像机设备（默认第一个）
    cv::VideoCapture cap= cv::VideoCapture(0);
    //2.一定要先设置采集格式！！！
    cap.set(CV_CAP_PROP_FOURCC, cv::VideoWriter::fourcc('M', 'J', 'P', 'G'));
    //3.然后再设置高清采集分辨率
    cap.set(CV_CAP_PROP_FRAME_WIDTH, 640);
    cap.set(CV_CAP_PROP_FRAME_HEIGHT, 480);


	//VideoCapture cap(0);
    if(!cap.isOpened())
    {
        cout <<"open camera error"<< endl;
        return -1;
    }
        cout <<"open camera ok"<< endl;
    CascadeClassifier Classifier("/usr/share/opencv/haarcascades/haarcascade_frontalface_alt2.xml");	
	vector<Rect> face;
    vector<uchar> jpgface;
	Mat ColorImage;
    Mat GrayImage;
	Mat EqualImage;
	Mat MatFace;

    string Base64Face;
    Json::Value result;

    std::string image_type = "BASE64";

    std::string group_id_list = "class";

    time_t sec;

	//Mat image2;
	while(1)
    {
        cap >> ColorImage;
	    cvtColor(ColorImage,GrayImage,COLOR_BGR2GRAY); // 彩色图转灰度图
	    equalizeHist(GrayImage,EqualImage); //直方图均衡化
        Classifier.detectMultiScale(EqualImage,face); // 人脸识别
        //pid_t id = fork();        
        //if(!id)
        //{
        if(face.size())
        {
        	rectangle(EqualImage,face[0],Scalar(255,255,255));// 截图
            string txt = "ok";
            //putText(EqualImage, txt, Point(10,10), FONT_HERSHEY_SIMPLEX, 2 , Scalar(255,100,255));




            MatFace = EqualImage (face[0]); // 框选人脸
            imencode(".jpg",MatFace,jpgface);
            Base64Face = base64_encode((char *)jpgface.data(), jpgface.size());
         
            //std::string image = Base64Face;
            
            //std::map<std::string, std::string> options;

            //options["liveness_control"] = "NORMAL";


            // 调用人脸搜索
            result = client.face_search_v3(Base64Face,image_type,group_id_list ,(Json::Value&)aip::null);

            if(!result["result"].isNull())
            {
                if(result["result"]["user_list"][0]["score"].asInt()  >  50)
                {
                    cout << result["result"]["user_list"][0]["user_id"] << endl ;
                    sec = time(NULL);
                    cout << ctime(&sec) << endl ;
                    string name =  result["result"]["user_list"][0]["user_id"].asString()  ;
                    putText(EqualImage, name, Point(150,100), FONT_HERSHEY_PLAIN, 3 , Scalar(255,100,255));
                    putText(EqualImage, ctime(&sec), Point(150,200), FONT_HERSHEY_PLAIN, 1 , Scalar(255,100,255));

                }
            }
            //cout << result << endl;

        }
            imshow("video" ,EqualImage);
            waitKey(100);    
           // exit(0);
        
        //}
             // pid_t ret_id = wait(NULL);

	}

	return 0;    

}
